from datetime import datetime

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(20),unique=True)
    # gender = db.Column(db.Boolean,default=False)
    password = db.Column(db.String(20))
    # age = db.Column(db.Integer,nullable=True)
    regdate = db.Column(db.DateTime,default=datetime.now)
    articles = db.relationship('Article',backref='u')

    __tablename__ = 'user'

    def save(self):
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return '<user :{}>'.format(self.name)

class Article(db.Model):

    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(50))
    content = db.Column(db.Text,default='')
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    # keywords_id = db.Column(db.String(20),db.ForeignKey('cate.id'),default='')

a_c = db.Table('a_c',
            db.Column('article_id',db.Integer,db.ForeignKey('article.id')),
            db.Column('category_id',db.Integer,db.ForeignKey('cate.id'))
)

class Category(db.Model):
    """文章标签"""
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    content = db.Column(db.String(20),)
    articles = db.relationship('Article', secondary=a_c,
                           backref='article', lazy='dynamic')

    __tablename__ = 'cate'